package com.yykj.hospital.server.service.impl;

import com.yykj.common.constant.RedisConstant;
import com.yykj.common.enums.ResultEnums;
import com.yykj.common.exception.NormalException;
import com.yykj.common.service.RedisService;
import com.yykj.common.util.LocalStringUtil;
import com.yykj.common.vo.ResultVO;
import com.yykj.hospital.server.dto.DoctorPasswordDTO;
import com.yykj.hospital.server.dto.PayPasswordCodeForm;
import com.yykj.hospital.server.dto.PayPasswordSetForm;
import com.yykj.hospital.server.model.entity.DoctorPassword;
import com.yykj.hospital.server.model.repository.DoctorPasswordRepository;
import com.yykj.hospital.server.service.DoctorPasswordService;
import com.yykj.hospital.server.service.bank.BankService;
import com.yykj.message.center.client.dto.MessageDTO;
import com.yykj.message.center.client.feign.MessageFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DoctorPasswordServiceImpl implements DoctorPasswordService {
    @Override
    public DoctorPasswordDTO checkPayPassword(String doctorId) {
        DoctorPassword doctorPassword = doctorPasswordRepository.findById(doctorId).orElse(null);
        DoctorPasswordDTO ret = new DoctorPasswordDTO();
        ret.setDoctorId(doctorId);
        ret.setHavePwd(doctorPassword != null);
        return ret;
    }

    @Override
    public boolean checkPayPasswordRight(String doctorId, String password) {
        DoctorPassword doctorPassword = doctorPasswordRepository.findById(doctorId).orElse(null);
        bankService.checkPayPasswordCnt(doctorPassword,password);
        return doctorPassword != null && !StringUtils.isEmpty(doctorPassword.getPayPwd()) && doctorPassword.getPayPwd().equals(LocalStringUtil.encrypt(password));
    }

    @Override
    public void sendPayCheckCode(PayPasswordCodeForm sendForm) {
        if(StringUtils.isEmpty(sendForm.getMobile())){
            throw new NormalException(ResultEnums.PARAM_ERROR);
        }
        String code = getPayCode(sendForm.getMobile());
        if (code != null) {
            throw new NormalException(ResultEnums.CODE_NOT_EXPIRE);
        }
        ResultVO<MessageDTO> ret = messageFeignClient.getCode(sendForm.getMobile());
        if (ret.getCode().equals(0)) {
            code = ret.getData().getCode();
            setPayCode(sendForm.getMobile(), code);
        } else {
            throw new NormalException(ResultEnums.LOGIN_FAIL, ret.getMsg());
        }
    }

    @Override
    public void payPasswordSetting(PayPasswordSetForm form) {
        if(StringUtils.isEmpty(form.getPassword()) || !form.getPassword().equals(form.getRePassword())){
            throw new NormalException(ResultEnums.RE_PASSWORD_ERROR);
        }
        DoctorPassword doctorPassword = doctorPasswordRepository.findById(form.getDoctorId()).orElse(null);
        if(doctorPassword ==null){
            doctorPassword = new DoctorPassword();
            doctorPassword.setDoctorId(form.getDoctorId());
        }

        doctorPassword.setPayPwd(LocalStringUtil.encrypt(form.getPassword()));
        doctorPasswordRepository.save(doctorPassword);
    }

    @Override
    public boolean checkPayCheckCode(PayPasswordCodeForm sendForm) {
        String payCode = getPayCode(sendForm.getMobile());
        return payCode != null && payCode.equals(sendForm.getCode());
    }

    @Override
    public void payPasswordResetting(PayPasswordSetForm form) {
        if(StringUtils.isEmpty(form.getPassword()) || !form.getPassword().equals(form.getRePassword())){
            throw new NormalException(ResultEnums.RE_PASSWORD_ERROR);
        }
        DoctorPassword doctorPassword = doctorPasswordRepository.findById(form.getDoctorId()).orElse(null);
        if(doctorPassword ==null){
           throw new NormalException(ResultEnums.PAY_PASSWORD_NOT_SET_ERROR);
        }
        if(!doctorPassword.getPayPwd().equals(LocalStringUtil.encrypt(form.getOldPassword()))){
            throw new NormalException(ResultEnums.OLD_PASSWORD_ERROR);
        }

        doctorPassword.setPayPwd(LocalStringUtil.encrypt(form.getPassword()));
        doctorPasswordRepository.save(doctorPassword);
    }


    private void setPayCode(String mobile, String code) {
        String key = "pay_code_" + mobile;
        redisService.set(key, code, RedisConstant.MESSAGE_EXPIRE);
    }

    private String getPayCode(String mobile) {
        String key = "pay_code_" + mobile;
        return redisService.get(key);
    }


    private DoctorPasswordRepository doctorPasswordRepository;

    private MessageFeignClient messageFeignClient;

    private BankService bankService;


    @Autowired
    public void setDoctorPasswordRepository(DoctorPasswordRepository doctorPasswordRepository) {
        this.doctorPasswordRepository = doctorPasswordRepository;
    }

    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    @Autowired
    public void setMessageFeignClient(MessageFeignClient messageFeignClient) {
        this.messageFeignClient = messageFeignClient;
    }
    @Autowired
    public void setBankService(BankService bankService) {
        this.bankService = bankService;
    }
}
